﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;

namespace Our.Umbraco.uDbCompare
{
    public class uDbCompareCMSNode : CMSNode
    {
        private static Guid objectType = new Guid("{CB0A3562-E53D-4847-AEC3-72EB7789A398}");
        
        public static bool Delete(int nodeId)
        {
            if (nodeId > 0)
            {
                var node = new CMSNode(nodeId);
                if (node != null)
                {
                    foreach (CMSNode child in node.GetDescendants())
                    {
                        child.delete();
                    }
                    node.delete();
                    return true;
                }
            }
            return false;
        }

        public static CMSNode MakeNew(int userId, string text)
        {
            return MakeNew(-1, userId, 1, text);
        }

        public static CMSNode MakeNew(int parentId, int userId, int level, string text)
        {
            // Create the new DbCompare Node
            CMSNode node = CMSNode.MakeNew(parentId, objectType, userId, level, text, Guid.NewGuid());
            CMSNode settingNode = CMSNode.MakeNew(node.Id, objectType, userId, level + 1, text, Guid.NewGuid());

            return node;
        }

        public static string GetConnectionName(int nodeId)
        {
            CMSNode node = new CMSNode(nodeId);
            return node.Text;
        }

        public static string GetConnectionString(int nodeId)
        {
            if (nodeId > 0)
            {
                CMSNode node = new CMSNode(nodeId);
                foreach (var child in node.GetDescendants())
                {
                    var childNode = child as CMSNode;
                    if (childNode != null)
                        return childNode.Text;
                }
            }
            return string.Empty;
        }

        public static void SaveConnectionName(int nodeId, string connectionName)
        {
            CMSNode node = new CMSNode(nodeId);
            node.Text = connectionName;
        }

        public static void SaveConnectionString(int nodeId, string connectionString)
        {
            bool found = false;
            if (nodeId > 0)
            {
                CMSNode node = new CMSNode(nodeId);
                foreach (var child in node.GetDescendants())
                {
                    var childNode = child as CMSNode;
                    if (childNode != null)
                    {
                        childNode.Text = connectionString;
                        found = true;
                    }
                }
                if (!found)
                {
                    CMSNode settingNode = CMSNode.MakeNew(node.Id, objectType, 0, 2, connectionString, Guid.NewGuid());
                }
            }
        }

        public static List<CMSNode> GetAllNodes()
        {
            List<CMSNode> allNodes = new List<CMSNode>();
            int[] ids = CMSNode.getAllUniqueNodeIdsFromObjectType(objectType);
            foreach (var id in ids)
            {
                var node = new CMSNode(id);
                if (node.Level == 1)
                    allNodes.Add(node);
            }
            return allNodes;
        }
    }
}
